﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CHPM.IC.ServiceLocator;
using CHPM.PL.DesktopUI.Code.Validation;
using CHPM.PL.DesktopUI.Interfaces;
using CHPM.PL.DesktopUI.ServiceReference1;

namespace CHPM.PL.DesktopUI.Presenters
{
    public class EditPatientPresenter : BasePresenter
    {
        private IEditPatient _editPatientView;
        private IMainService _mainService;
        private PATIENT _patient;
        //private ErrorProvider _errorProvider;

        #region Constructors
        public EditPatientPresenter(IServiceLocator serviceLocator, IEditPatient view, int id):base(serviceLocator)
        {
            _editPatientView = view;
            _mainService = serviceLocator.Resolve<IMainService>();
            _patient = _mainService.GetPatientById(id);
            _editPatientView.FormLoadExt += new EventHandler(_editPatientView_FormLoadExt);
           
            _editPatientView.PatientSaving += PatientSender;

            //_editPatientView.AdressValidating += NewPatientValidator.AdressValidation;
        }

        void _editPatientView_FormLoadExt(object sender, EventArgs e)
        {
            SetDataSource(_patient);
        }

        public void PresentEditPatient(Control container)
        {
            SetDataSource(_patient);
            this.ClearControls(container);
            container.Controls.Add(_editPatientView.AsControl);
            container.Controls[container.Controls.Count - 1].Dock = DockStyle.Fill;
        }

        #endregion

        public void SetDataSource(PATIENT patient)
        {
            _editPatientView.FullName = patient.FULLNAME;
            _editPatientView.Address = patient.ADRESS;
            _editPatientView.Card = patient.CARD;
            _editPatientView.Phone = patient.PHONE;
            _editPatientView.Birthday = patient.BIRTHDATE;
            _editPatientView.EnterDate = patient.ENTER_DATE;
            _editPatientView.Invalid = 1;//Int32.Parse(patient.INVAL.ToString());
            _editPatientView.TreatmentCourse = patient.ID_TREATMENT_COURSE;
            _editPatientView.Ambulatory = patient.AMBULATORY;
            _editPatientView.Sex = patient.GENDER;
            _editPatientView.Chornobyl = bool.Parse(patient.IS_CHORNOBUL.ToString());
            _editPatientView.Citizen = patient.IS_CITIZEN;
            _editPatientView.Sanatoriy = patient.IS_SANATORY;
        }

        public void PatientSender(Object sender, EventArgs args)
        {
            PATIENT newPatient = new PATIENT();
            newPatient.Id = _patient.Id;
            newPatient.FULLNAME = _editPatientView.FullName;
            newPatient.ADRESS = _editPatientView.Address;
            newPatient.CARD = _editPatientView.Card;
            newPatient.PHONE = _editPatientView.Phone;
            newPatient.BIRTHDATE = _editPatientView.Birthday;
            newPatient.ENTER_DATE = _editPatientView.EnterDate;
            newPatient.INVAL = _editPatientView.Invalid;
            newPatient.ID_TREATMENT_COURSE = _editPatientView.TreatmentCourse;
            newPatient.AMBULATORY = _editPatientView.Ambulatory;
            newPatient.GENDER = _editPatientView.Sex;
            newPatient.IS_CHORNOBUL = _editPatientView.Chornobyl;
            newPatient.IS_CITIZEN = _editPatientView.Citizen;
            newPatient.IS_SANATORY = _editPatientView.Sanatoriy;

            _mainService.UpdatePatient(newPatient);
        }



    }
}
